package com.xingdata.jjxc.activity;

import jar.tools.UdpTools;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.adapter.DrivingHabitsAdapter;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.OBDEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.SystemInfo;

public class DrivingHabitsListView extends Base1Activity {
	private ListView mListView;
	private ArrayList<OBDEntity> arrayList = new ArrayList<OBDEntity>();
	private DrivingHabitsAdapter adapter;

	@Override
	protected int getContentViewId() {
		return R.layout.drivinghabits_listview;
	}

	@Override
	protected String getTitleText() {
		if (getIntent().getExtras() != null) {
			//return "行驶记录";
			return "里程记录详情";
		} else {
			return "驾驶习惯";
		}

	}

	@Override
	protected void initViews() {
		mListView = (ListView) findViewById(R.id.drivinghabits_listview);
		adapter = new DrivingHabitsAdapter(this, arrayList);
		mListView.setAdapter(adapter);

		if (getIntent().getExtras() != null) {
			arrayList.addAll((Collection<? extends OBDEntity>) getIntent()
					.getExtras().getSerializable("entity"));
			adapter.notifyDataSetChanged();
		} else {
			findobdDriver();
		}
	}

	private void findobdDriver() {
		paramsMap = new HashMap<String, String>();
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
		if (!StringUtil.isStringNull(UdpTools.getHudCode())) {
			paramsMap.put("hud_sn",
					SharedPreTools.getString(this, Common.HUD_SN, ""));
		} else {
			paramsMap.put("hud_sn", UdpTools.getHudCode());
		}

		HttpUtil.getInstance(this).Post(App.ZZD_REQUEST_FINDOBDDRIVER,
				paramsMap, new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
						
						arrayList.addAll(JSON.parseArray(
								respEntity.getResult(), OBDEntity.class));
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onLoading(int vurrent) {

					}

					@Override
					public void onFailure(String result) {
						showToast(result);
					}
				});
	}

}
